<?php

namespace App\Http\Controllers\user;

use App\Http\Controllers\Controller;
use App\Http\Logic\LoginLogic;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\Http;

class LoginController extends Controller
{
    public function loginOrRegister(Request $request)
    {
        $code = $request->input('code');

        $nickname = $request->input('nickname');

        $avatar = $request->input('avatar');

        $user = LoginLogic::login($code, $nickname, $avatar);

        $token = $user->createToken('miniapp')->plainTextToken;

        return $this->success(['token' => $token, 'user' => $user]);
    }

    public function phoneLogin(Request $request)
    {
        $params = $request->all();
        $user = LoginLogic::mpLogin($params);
        $token = $user->createToken('miniapp')->plainTextToken;
        return $this->success(['token' => $token, 'user' => $user]);
    }

    public function logout(Request $request)
    {
        $request->user()->currentAccessToken()->delete();
        return $this->success(['message' => '登出成功']);
    }

}
